********************************************************************************
* MANAGE IoT RAW and create final_consumption
* File description: Managing IoT, KPI etc. creating the correct variables 
********************************************************************************

local cpi2012 1.00709739
local cpi2011 1.016054972
local cpi2010 1.042740394
local cpi2009 1.05596342521524
local cpi2008 1.05262632646951
local cpi2007 1.08922240198272
local cpi2006 1.11332770389135
local cpi2005 1.12849500713267
local cpi2004 1.1333452722063
local cpi2003 1.13782811938152
local cpi2002 1.15993401759531
local cpi2001 1.18468738300262
local cpi2000 1.21377062
local cpi1999 1.22599768

forv j=1999/2012 {
    di `j'
use LopNr bkon bkuinst bkungr cdisp cdisph cfviki ctxfvi nrv kakurta kkurta kkuvp kkutomt tkassa binvar* using "raw/IoT/iot`j'.dta", clear

cap drop binvarm
cap ren binvarut binvar	
desc binvar*
duplicates drop
duplicates drop LopNr, force

gen year=`j'

foreach var in cdisp cdisph cfviki ctxfvi nrv kakurta kkurta tkassa {
	
replace `var'=`var'*`cpi`j''

}

destring bkon binvar, replace
tostring bkuinst bkungr, replace

mvencode binvar, mv(0)o
g fborn=cond(binvar>0,1,0)
label var fborn "Foreign born"
label var binvar "Immigration Date"
label var nrv "Business income"

if `j'==1999 {
save "use/iot_temp", replace
}

else {
append using "use/iot_temp"
save "use/iot_temp", replace
}

}

gen married=0
replace married=1 if marital_status==7 /*Wife*/
replace married=1 if marital_status==3 /*Husband*/
tab married
drop marital_status

rename cdisp dispinc
rename cdisph dispinch

gen lndispinc=log(dispinc)
gen lndispinch=log(dispinch)

*Create municipality income variables

preserve

collapse (p90) p90=dispinch (p80) p80=dispinch (p50) p50=dispinch (p20) p20=dispinch (p25) p25=dispinch (p75) p75=dispinch, by(municipality year)

tempfile topinc
save `topinc'

restore

merge m:1 municipality year using `topinc', keep(3) nogen

* Create individual variables

gen age2=age*age

rename education_level educ

gen low_educ=cond(educ<=1, 1, 0)

gen unemployed=cond(tkassa>0,1,0)

gen selfemployed=cond(nrv>0,1,0)

gen poor=cond(dispinch<0.6*p50, 1, 0)

* Create municipality control variables

bys municipality year: egen share_loweduc=mean(low_educ)

bys municipality year: egen avg_age=mean(age)

bys municipality year: egen share_female=mean(female)

bys municipality year: egen share_kids=mean(children)

bys municipality year: egen share_married=mean(married)

bys municipality year: egen share_selfemployed=mean(selfemployed)

bys municipality year: egen share_poor=mean(poor)

bys municipality year: gen population=_N

* Create family variables

bys LopNr_family year: egen max_age=max(age)
gen head=1 if age==max_age

bys LopNr_family year: gen max_inc=cond(dispinc>=0.5*dispinch,1,0)

keep if head==1

bys LopNr_family year: egen sum_head=sum(head)

sum sum_head

gen head2=cond(max_inc==1,1,0)

drop if head2==0 & sum_head==2

bys LopNr_family year: egen sum_head2=sum(head)

sum sum_head2

count if head2>1

duplicates drop 

duplicates drop LopNr_family year, force

* Merge

merge 1:1 LopNr_family year using "use/yitz_1999_2012_hh", keep(3) nogen 

rename LopNr_family idfam 

merge 1:1 idfam year using "use/consumption", keep(1 3) nogen	

replace municipality=331 if municipality==1917

merge m:1 municipality year using "use/houseprices", keep(1 3) nogen	

drop kkurta ctxfvi kkuvp kakurta kkutomt bkuinst bkungr tkassa cfviki nrv binvar bkon head2 sum_head2 head

gen lnp90=log(p90)
gen lnp80=log(p80)
gen lnp50=log(p50)
gen lnp20=log(p20)
gen lnp75=log(p75)
gen lnp25=log(p25)

label var lnp80 "Log(P80)"

drop if year<=1999

* Restrictions
drop if dispinc<0
drop if dispinch<0
drop if age<15
drop if mi(consumptionh)
drop if consumptionh<=0

* Generate variables for analysis
drop lndispinch
gen savings=dispinch-consumptionh
gen lndispinch=log(dispinch)
gen lndispinch2=log(dispinch*dispinch)
gen lnsavings=log(dispinch-consumptionh)

destring municipality, replace

replace yitz_muni=yitz_muni/100000

save "use/final_consumption", replace

